﻿namespace zijian666.Data;

/// <summary>
/// 动态类型: 行数据
/// </summary>
#pragma warning disable CS0660 // 类型定义运算符 == 或运算符 !=，但不重写 Object.Equals(object o)
#pragma warning disable CS0661 // 类型定义运算符 == 或运算符 !=，但不重写 Object.GetHashCode()
internal class DynamicDataRow(DataRow row) : DynamicDictionary(row)
{
    private readonly DataRow _row = row;

    public override IEnumerable<string> GetDynamicMemberNames()
        => _row.Table.Columns.Cast<DataColumn>().Select(x => x.ColumnName);

    protected override DynamicAtom this[int columnIndex]
    {
        get
        {
            var col = _row.Table.Columns[columnIndex];
            if (col == null)
            {
                return NULL;
            }
            return new DynamicDataCell(_row, col);
        }
    }

    protected override DynamicAtom this[string columnName]
    {
        get
        {
            var col = _row.Table.Columns[columnName];
            if (col == null)
            {
                return NULL;
            }
            return new DynamicDataCell(_row, col);
        }
    }
    protected override bool TryGetValue(string name, out object? value)
    {
        if (_row.Table.Columns.Contains(name))
        {
            value = _row[name];
            return true;
        }
        else
        {
            value = null;
            return false;
        }
    }

    #region Equals
    public static bool operator ==(DynamicDataRow a, object b) => a?.Equals(b) ?? b == null;

    public static bool operator !=(DynamicDataRow a, object b) => !(a?.Equals(b) ?? b == null);

    #endregion
}
